
package tema6;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name="GenerarFormularioAtributosServlet", urlPatterns={"/tema6/generarFormularioAtributos"})
public class GenerarFormularioAtributosServlet extends HttpServlet {
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        HashMap<String,String> atributosPeticion = getAtributosPeticion(request);
        HttpSession sesion = request.getSession();
        HashMap<String,String> atributosSesion = getAtributosSesion(sesion);
        ServletContext contexto = request.getServletContext();
        HashMap<String,String> atributosContexto = getAtributosContexto(contexto);
        Cookie[] cookies = request.getCookies();
        HashMap<String,String> atributosCookies = getAtributosCookies(cookies);
        
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<link rel=\"stylesheet\" title=\"standar\" type=\"text/css\" href=\"/EjemploBasicoJavaEE/standar.css\">");
            out.println("<title>Lista todos los Atributos de la aplicacion</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Todos los Atributos de la aplicación</h1>");
            out.println("<h2>Deseleccione los que desee eliminar</h2>");
            
            out.println("<h3>Atributos de la <b>PETICIÓN</b>:</h3>");
            imprimeAtributosHashMap(out,atributosPeticion);
            out.println("<form method=\"post\" action=\"/EjemploBasicoJavaEE/tema6/modificarAtributos\" name=\"modificarAtributos\">");
            out.println("<h3>Atributos de la <b>SESIÓN</b>:</h3>");
            checkboxAtributosHashMap(out,"sesion",atributosSesion);
            out.println("<h3>Atributos del <b>CONTEXTO</b>:</h3>");
            checkboxAtributosHashMap(out,"contexto",atributosContexto);
            out.println("<h3>Atributos de las <b>COOKIES</b>:</h3>");
            checkboxAtributosHashMap(out,"cookies",atributosCookies);           
            out.println("<button>Modificar Atributos</button>");            
            out.println("</form>");
            
            out.println("<br><a href=\"/EjemploBasicoJavaEE/tema6/formularioModificarAtributos.html\">No quiero modificar nada. Llévame atrás</a>");
            out.println("</body>");
            out.println("</html>");
            
        } finally { 
            out.close();
        }
    } 

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    } 

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    
    private void imprimeAtributosHashMap(PrintWriter out,HashMap<String, String> atributos) {
        out.println("<ul>");
        for(String atributo : atributos.keySet()) {
            out.println("<li><b>"+atributo+"</b> = "+atributos.get(atributo) +"</li>");
        }
        out.println("</ul>");
    }
    
    private void checkboxAtributosHashMap(PrintWriter out,String categoria, HashMap<String, String> atributos) {
        for(String atributo : atributos.keySet()) {
            out.println("<input type=\"checkbox\" name=\""+categoria+"\" value=\""+atributo+
                    "\" checked><b>"+atributo+"</b> = "+atributos.get(atributo)+"<br>");
        }
    }

    private HashMap<String,String> getAtributosPeticion(HttpServletRequest request) {
        HashMap<String,String> atributos = new HashMap();
        Enumeration<String> cabecerasAtributos = request.getAttributeNames();
        while(cabecerasAtributos.hasMoreElements()){
            String atributo = cabecerasAtributos.nextElement();
            atributos.put(atributo, (String) request.getAttribute(atributo));
        }
        
        return atributos;
    }

    private HashMap<String, String> getAtributosSesion(HttpSession sesion) {
        HashMap<String,String> atributos = new HashMap();
        Enumeration<String> cabecerasAtributos = sesion.getAttributeNames();
        while(cabecerasAtributos.hasMoreElements()){
            String atributo = cabecerasAtributos.nextElement();
            atributos.put(atributo, (String) sesion.getAttribute(atributo));
        }
        
        return atributos;
    }

    private HashMap<String, String> getAtributosContexto(ServletContext contexto) {
        HashMap<String,String> atributos = new HashMap();
        Enumeration<String> cabecerasAtributos = contexto.getAttributeNames();
        while(cabecerasAtributos.hasMoreElements()){
            String atributo = cabecerasAtributos.nextElement();
            atributos.put(atributo, contexto.getAttribute(atributo).toString());            
        }
        
        return atributos;
    }

    private HashMap<String, String> getAtributosCookies(Cookie[] cookies) {
        HashMap<String,String> atributos = new HashMap();
        for(int i=0;cookies!=null&&cookies.length>i;i++){
            atributos.put(cookies[i].getName(), cookies[i].getValue());
        }
        
        return atributos;
    }

}
